package dtos

import (
	"encoding/json"
	"strconv"

	"cashew.com/cashew_common/constants"
	"github.com/beego/beego/v2/core/validation"
	beecontext "github.com/beego/beego/v2/server/web/context"
)

func Bind[T any](params *T, ctx *beecontext.Context) error {
	body := ctx.Input.RequestBody

	// 从请求体中解析参数
	if err := json.Unmarshal(body, params); err != nil {
		return err
	}

	err := ValidateParams(params)

	if err != nil {
		return err
	}

	return nil
}

type AnchorGetAnchorInfoParams struct {
	UserId    int64
	UserIdStr string `json:"user_id"`
}

func (c *AnchorGetAnchorInfoParams) Valid(v *validation.Validation) {
	// 验证UserId是否合法
	var err error
	c.UserId, err = strconv.ParseInt(c.UserIdStr, 10, 64)

	if err != nil {
		v.SetError("UserId", "用户编号不合法")
		return
	}

	if c.UserId <= 0 {
		v.SetError("UserId", "用户编号必须是正整数")
	}

	return
}

type AnchorGetAnchorInfoResult struct {
	UserId        string      `json:"user_id"`
	MainRegionId  int         `json:"main_region_id"`
	SubRegionId   int         `json:"sub_region_id"`
	Status        int         `json:"status"`
	DisableStatus int8        `json:"disable_status"`
	LiveModel     int         `json:"live_model"`
	Price         []PriceData `json:"price_data"`
}

type PriceData struct {
	PriceType string `json:"price_type"`
	PayPrice  int    `json:"pay_price"`
}

type AnchorUpdateAnchorInfoParams struct {
	UserId      int64
	UserIdStr   string `json:"user_id"`
	CountryCode string `json:"country_code"`
}

func (c *AnchorUpdateAnchorInfoParams) Valid(v *validation.Validation) {
	// 验证UserId是否合法
	var err error
	c.UserId, err = strconv.ParseInt(c.UserIdStr, 10, 64)

	if err != nil {
		v.SetError("UserId", "用户编号不合法")
		return
	}

	if c.UserId <= 0 {
		v.SetError("UserId", "用户编号必须是正整数")
	}

	// 验证CountryCode是否合法
	if c.CountryCode == "" {
		v.SetError("CountryCode", "国家代码不能为空")
		return
	}

	return
}

type AnchorGetVideoCallMaterialsParams struct {
	UserId    int64
	UserIdStr string `json:"user_id"`
}

func (c *AnchorGetVideoCallMaterialsParams) Valid(v *validation.Validation) {
	// 验证UserId是否合法
	var err error
	c.UserId, err = strconv.ParseInt(c.UserIdStr, 10, 64)

	if err != nil {
		v.SetError("UserId", "用户编号不合法")
		return
	}

	if c.UserId <= 0 {
		v.SetError("UserId", "用户编号必须是正整数")
	}

	return
}

type AnchorGetVideoCallMaterialsResult struct {
	UserId               string             `json:"user_id"`
	MaterialsAuditStatus int8               `json:"materials_audit_status"`
	Reason               string             `json:"reason"`
	MaterialList         []MaterialListItem `json:"material_list"`
}

type MaterialListItem struct {
	Id            int    `json:"id"`
	FileHost      string `json:"file_host"`
	MaterialsType int    `json:"materials_type"`
	Msg           string `json:"msg"`
	MsgType       int    `json:"msg_type"`
	Duration      int    `json:"duration"`
}

type AnchorAddVideoCallMaterialsAuditParams struct {
	UserId       int64
	UserIdStr    string             `json:"user_id"`
	CountryCode  string             `json:"country_code"`
	MaterialList []MaterialListItem `json:"material_list"`
}

func (c *AnchorAddVideoCallMaterialsAuditParams) Valid(v *validation.Validation) {
	// 验证UserId是否合法
	var err error
	c.UserId, err = strconv.ParseInt(c.UserIdStr, 10, 64)

	if err != nil {
		_ = v.SetError("UserId", "用户编号不合法")
		return
	}

	if c.UserId <= 0 {
		_ = v.SetError("UserId", "用户编号必须是正整数")
	}

	if c.CountryCode == "" {
		_ = v.SetError("CountryCode", "国家代码不能为空")
		return
	}

	if len(c.MaterialList) < 3 {
		_ = v.SetError("MaterialList", "资料不能少于3个")
		return
	}

	if len(c.MaterialList) > 10 {
		_ = v.SetError("MaterialList", "资料不能多于10个")
		return
	}

	avatarCount := 0
	picCount := 0
	materialCount := 0

	// 验证MaterialList中的Id是否合法
	for _, item := range c.MaterialList {
		if !constants.ValidateMaterialType(item.MaterialsType) {
			_ = v.SetError("MaterialsType", "资料类型不合法")
			return
		}
		if !constants.ValidateMaterialContentType(item.MsgType) {
			_ = v.SetError("MsgType", "资料内容类型不合法")
			return
		}
		if item.Msg == "" {
			_ = v.SetError("content", "资料内容不能为空")
			return
		}
		if item.MaterialsType == constants.MATERIAL_TYPE_AVATAR {
			if item.MsgType != constants.MATERIAL_CONTENT_TYPE_PIC {
				_ = v.SetError("MsgType", "头像内容类型错误"+strconv.Itoa(item.MsgType))
				return
			}
			avatarCount++
		} else if item.MaterialsType == constants.MATERIAL_TYPE_PIC {
			if item.MsgType != constants.MATERIAL_CONTENT_TYPE_PIC {
				_ = v.SetError("MsgType", "认证图片内容类型错误"+strconv.Itoa(item.MsgType))
				return
			}
			picCount++
		} else if item.MaterialsType == constants.MATERIAL_TYPE_MATERIAL {
			materialCount++
		}
	}

	// 验证头像数量
	if avatarCount != 1 {
		_ = v.SetError("MaterialList", "头像数量错误"+strconv.Itoa(avatarCount))
		return
	}

	// 验证认证图片数量
	if picCount != 1 {
		_ = v.SetError("MaterialList", "认证图片数量错误"+strconv.Itoa(picCount))
		return
	}

	// 验证资料数量
	if materialCount < 1 || materialCount > 8 {
		_ = v.SetError("MaterialList", "问候语数量错误"+strconv.Itoa(materialCount))
		return
	}

	return
}

type AnchorAddVideoCallMaterialsAuditResult struct {
	IsFirstSubmit bool `json:"is_first_submit"`
}

type AnchorGetSysPriceListParams struct {
	UserId    int64
	UserIdStr string `json:"user_id"`
}

func (c *AnchorGetSysPriceListParams) Valid(v *validation.Validation) {
	// 验证UserId是否合法
	var err error
	c.UserId, err = strconv.ParseInt(c.UserIdStr, 10, 64)

	if err != nil {
		_ = v.SetError("UserId", "用户编号不合法")
		return
	}

	if c.UserId <= 0 {
		_ = v.SetError("UserId", "用户编号必须是正整数")
	}

	return
}

type AnchorGetSysPriceListResult struct {
	ConfigList []SysPriceItem `json:"config_list"`
}

type SysPriceItem struct {
	Id          int  `json:"id"`
	AnchorPrice int  `json:"anchor_price"`
	IsInUse     bool `json:"is_in_use"`
}

type AnchorUpdateVideoCallPriceParams struct {
	UserId        int64
	UserIdStr     string `json:"user_id"`
	PriceConfigId int    `json:"price_config_id"`
}

func (c *AnchorUpdateVideoCallPriceParams) Valid(v *validation.Validation) {
	// 验证UserId是否合法
	var err error
	c.UserId, err = strconv.ParseInt(c.UserIdStr, 10, 64)

	if err != nil {
		_ = v.SetError("UserId", "用户编号不合法")
		return
	}

	if c.UserId <= 0 {
		_ = v.SetError("UserId", "用户编号必须是正整数")
	}

	if c.PriceConfigId <= 0 {
		_ = v.SetError("PriceConfigId", "价格配置编号不合法")
		return
	}

	return
}

type CompleteUserInfoParams struct {
	UserId    int64
	UserIdStr string `json:"user_id"`
	Gender    int    `json:"gender"`
	Nick      string `json:"nick"`
	HeadImg   string `json:"head_img"`
	Age       int    `json:"age"`
}

func (cuip *CompleteUserInfoParams) Valid(v *validation.Validation) {
	// 验证UserId是否合法
	var err error
	cuip.UserId, err = strconv.ParseInt(cuip.UserIdStr, 10, 64)

	if err != nil {
		_ = v.SetError("UserId", "用户编号不合法")
		return
	}

	if cuip.UserId <= 0 {
		_ = v.SetError("UserId", "用户编号必须是正整数")
	}

	if cuip.Gender != 0 && cuip.Gender != constants.SEX_MAILE && cuip.Gender != constants.SEX_FEMALE {
		_ = v.SetError("gender", "性别不合法")
	}

	return
}

type UpdateLiveStatusParams struct {
	UserId      int64
	UserIdStr   string `json:"user_id"`
	LiveStatus  int    `json:"live_status"`
	LiveType    int    `json:"live_type"`
	SubRegionId int    `json:"sub_region_id"`
}

func (ulsp *UpdateLiveStatusParams) Valid(v *validation.Validation) {
	// 验证UserId是否合法
	var err error
	ulsp.UserId, err = strconv.ParseInt(ulsp.UserIdStr, 10, 64)

	if err != nil {
		_ = v.SetError("UserId", "用户编号不合法")
		return
	}

	if ulsp.UserId <= 0 {
		_ = v.SetError("UserId", "用户编号必须是正整数")
	}

	if ulsp.LiveStatus != constants.ANCHOR_LIVE_STATUS_LIVE && ulsp.LiveStatus != constants.ANCHOR_LIVE_STATUS_END {
		_ = v.SetError("LiveStatus", "直播状态错误")
	}

	if ulsp.SubRegionId < 0 {
		_ = v.SetError("SubRegionId", "国家编号不合法")
	}

	return
}
